/**
 * 日志模块服务
 */

const { addLog, searchLogs } = require('../db/logs');
const { LOGSTYPE } = require('../utils/CONST');

/**
 * 添加日志
 * @param {*} message 日志信息
 * @param {*} isError 是否为错误日志
 */
const add = async (message, isError = true) => {
    try {
        await addLog(message, isError ? LOGSTYPE.ERROR : LOGSTYPE.LOGIN);
    } catch (error) {
        console.log(error);
    }
}

/**
 * 搜索日志
 * @param {*} ctx 
 */
const search = async ctx => {
    try {
        const [list, total] = await searchLogs(ctx.request.query);
        ctx.body = {
            code: 1,
            data: list,
            total
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '日志查询失败'
        }
    }
}

module.exports = {
    add,
    search
}